package com.qsl.ssyx.order.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsl.ssyx.common.Interceptor.AuthContextHolder;
import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.model.order.OrderInfo;
import com.qsl.ssyx.order.service.OrderInfoService;
import com.qsl.ssyx.vo.order.OrderConfirmVo;
import com.qsl.ssyx.vo.order.OrderSubmitVo;
import com.qsl.ssyx.vo.order.OrderUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.auditing.AuditingHandler;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 青衫泪
 * @since 2023-09-08
 */
@Api(tags = "微信小程序-订单管理接口")
@RestController
@RequestMapping("/api/order")
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    @ApiOperation("确认订单（立即购买）")
    @GetMapping("/auth/confirmOrder")
    public Result confirm() {
        // 获取用户id
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;

        OrderConfirmVo orderConfirmVo = orderInfoService.confirmOrder(userId);
        return Result.ok(orderConfirmVo);
    }


    @ApiOperation("根据用户id-生成订单")
    @PostMapping("/auth/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitVo orderSubmitVo) {
        // 获取用户id
//        Long userId = AuthContextHolder.getUserId();
        Long userId = 1L;

        Long orderId = orderInfoService.submitOrder(orderSubmitVo, userId);
        return Result.ok(orderId);
    }

    @ApiOperation("根据orderNo（订单号）查询订单信息")
    @GetMapping("/inner/getOrderInfoByorderNo/{orderNo}")
    public OrderInfo getOrderInfoByorderNo(@ApiParam(value = "订单号", required = true) @PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfoByorderNo(orderNo);
        return orderInfo;
    }

    @ApiOperation("根据订单状态查询，订单信息") // 订单状态【0->待付款；1->待发货；2->已发货；3->待用户收货，已完成】
    @PostMapping("auth/findUserOrderPage/{page}/{limit}")
    public Result findUserOrderPage(@ApiParam(value = "当前页", required = true) @PathVariable Long page,
                                               @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                                               OrderUserQueryVo orderUserQueryVo) {
        Page<OrderInfo> pageParam = new Page<OrderInfo>(page, limit);
        Page<OrderInfo> orderInfoList = orderInfoService.findUserOrderByOrderStatusId(pageParam, orderUserQueryVo);
        return Result.ok(orderInfoList);
    }
}

